﻿//Begin Login Functions
var img = new Image;

function Login() {

}

Login.Initialize = function() {
    this.context = document.getElementById("LoginCanvas").getContext("2d");
}

Login.Draw = function () {
    this.context.drawImage(img, 400, 2, 386, 312, 0, 0, 386, 312);
}

Login.Start = function () {
    img.onload = function () {
        Login.Initialize();
        Login.Draw();
    }
    var imgurl = window.location.protocol + '//' + window.location.host + '/Images/SpriteSheets/009.png';
    img.src = imgurl;
}
//End Login Functions


//Begin Service Calls
function AuthenticateUser() {

    var varType = "POST";
    var varUrl = "services/AuthenticationService.svc/Login";
    var varData = '{"UserName": "' + $('#UserName').val() + '", "Password": "' + $('#Password').val() + '"}';
    var varContentType = "application/json; charset=utf-8";
    var varDataType = "json";
    var varProcessData = true;
    
    $.ajax({
        type: varType, //GET or POST or PUT or DELETE verb
        url: varUrl, // Location of the service
        data: varData, //Data sent to server
        contentType: varContentType, // content type sent to server
        dataType: varDataType, //Expected data format from server
        processdata: varProcessData, //True or False
        success: function (msg) {//On Successfull service call
            ServiceSucceeded(msg);
        },
        error: ServiceFailed// When Service call fails
    });
}

function ServiceSucceeded(result) {//When service call is sucessful
    if (result.LoginResult.ID > 0) {
        window.location = 'ConferenceRoom.aspx?ID=' + result.LoginResult.GUID;
    } else {
        var ErrMsgs = 'Error Logging in: \n';
        for (i = 0; i < result.LoginResult.ErrorMessages.length; i++) {
            ErrMsgs += ' ' + result.LoginResult.ErrorMessages[i] + ' \n';
        }

        alert(ErrMsgs);
    }
}
function ServiceFailed(result) {
    alert('Login Service call failed: ' + result.status + '' + result.statusText);
}